<?php
/**
 * Description of ManagingController
 * 用户管理
 * @author etpasse
 */
//require_rewrite(MODULE_ROOT."manager/admin/groupManager.class.php");
require_rewrite(MODULE_ROOT."manager/admin/LogManager.class.php");
require_rewrite(ROOT_PATH."data/UserConfig.class.php");
class ManagingController extends BaseController {

    private $method;
    //private $left_meun;
    public function __construct() {
        parent::__construct();
        //取得页面访问方式
        $this->method = Handler_http::getMethod();
    }

     /**
     * 查看用户
     */
    public function selUserList(){
        $userlist=new ManagingModule();
        if($this->method =="POST"){
            $sel['txtaudit'] = $_POST['txtaudit'];
            $sel['txtusername'] = $_POST['txtusername'];
            $this->tpl->assign('txtaudit', $sel['txtaudit']);
            $this->tpl->assign('txtusername', $sel['txtusername']);
        }
        $this->tpl->assign('userlist', $userlist->sql_user($sel,false));
        $this->tpl->display("managing/sel_user_list.html");
    }

    /*
     * 添加用户
     */
    public function addUser() {
       $group = new ManagingModule();
       $pwd_arr=  Handler_validate::getExpressions();
       if($this->method == "POST"){
                $user['username'] = $_POST['txtusername'];
                $password = $_POST['txtpassword'];
                $password_confirm = $_POST['txtpassword_confirm'];
                $user['email'] = $_POST['txtemail'];
                $user['phone'] = $_POST['txtmobile'];
                $user['iccard'] = $_POST['txtaudit'];
                $user['nickname'] = $_POST['nickname'];
                $user['groundid'] = $_POST['department'];//部门ID
                $user['level'] = $_POST['level'];
                $user['ip_adress'] = $_POST['ip_adress'];
                $user['addTime'] = time();
                $user['password'] = md5($password.ADMIN_KEY);
                if($password_confirm != $password ){
                    Handler_tool::alertAndRedirect('两次输入的密码不同，请重新输入！', HTTPFILEURL.'/managing/addUser/');
                }
                if(empty($user['iccard'])){
                    Handler_tool::alertAndRedirect('请输入IC卡号！', HTTPFILEURL.'/managing/addUser/');
                }
                if(!preg_match($pwd_arr['str'], $password) || strlen($password)!=intval($pwd_arr['length'])){
                    Handler_tool::alertAndRedirect('请输入指定格式组成的密码！', HTTPFILEURL.'/managing/addUser/');
                }
                switch($group->auth_user_add($user)){
                    case 1:
                        $this->auth_add_sql_log(1,$user['username']);
                        Handler_tool::alertAndRedirect('添加成功,等待审核授权！', HTTPFILEURL.'/managing/selUserList/');
                        break;
                    case 5:
                        Handler_tool::alertAndRedirect('该用户名已存在或正在审核中,请重新输入新用户名称！', HTTPFILEURL.'/managing/addUser/');
                        break;
                    default :
                        $this->auth_add_sql_log(5,$user['username']);
                         Handler_tool::alertAndRedirect('添加失败！', HTTPFILEURL.'/managing/addUser/');
                        break;
                }
       }
       $this->tpl->assign('pwdTxt',$pwd_arr);
       $this->tpl->assign('userlevel', UserConfig::$userlevel);
       $this->tpl->assign('dep_data', $group->sql_department_data());
       $this->tpl->display("managing/add_user.html");
    }
    /**
     * 批量导入用户
     */
    public function BulkImport(){
        $pwd_arr=  Handler_validate::getExpressions();
         $this->tpl->assign('pwdTxt',$pwd_arr);
        if ($this->method == "POST") {
            $row = new ManagingModule();
            //$d=$row->upload_user_csv($this->user['username']);
            $file = $_FILES["upload"];

            if($file['error'] ==4){
                Handler_tool::alertAndRedirect('请选择要导入的文件！', HTTPFILEURL.'/managing/BulkImport/');
            }else
                $sub=substr($file['name'],-3);
                if($sub!="csv"){
                    Handler_tool::alertAndRedirect('导入文件格式错误！', HTTPFILEURL.'/managing/BulkImport/');
                }
                setlocale(LC_ALL, array('zh_CN.gbk', 'zh_CN.gb2312', 'en_US.utf8','zh_CN.UTF8'));
                $open_file = fopen($file['tmp_name'], 'r');
                while ($data = fgetcsv($open_file)) { //每次读取CSV里面的一行内容
                        if($data[0] != 'id' && $data[1] != 'name' && $data[2] != 'pwd' && $data[3]!='icid'){
                            if(!preg_match($pwd_arr['str'], $data[2]) || strlen($data[2])!=intval($pwd_arr['length'])){
                                    Handler_tool::alertAndGoBack('导入文件密码格式错误，'.$pwd_arr['txt'].','.$pwd_arr['length'].'位组成的密码！');
                                    exit;
                                }
                            $user['username'] = $data[0];
                            $user['email'] = '';
                            $user['phone'] = '';
                            $user['iccard'] = $data[3];
                            $user['nickname'] = iconv('gb2312','utf-8',$data[1]);
                            $user['is_audit'] = '0';
                            $user['groundid'] = $data[5];
                            $user['level'] = $data[4];
                            $user['ip_adress'] = '';
                            $user['password'] = md5($data[2].ADMIN_KEY);
                            $user['addTime'] = time();

                            $return_coupon_id=$row->auth_user_add($user);
                            $this->auth_add_sql_log(4,$data[0]);
                        }

                    }
                    if(!$return_coupon_id){
                        Handler_tool::alertAndRedirect('向数据库中导入失败,请重新导入！', HTTPFILEURL.'/managing/BulkImport/');
                    }else{
                        Handler_tool::alertAndRedirect('文件导入成功！', HTTPFILEURL.'/managing/BulkImport/');
                    }
        }
        $this->tpl->display("managing/bulk_import.html");
    }

    /*
     * 修改用户信息
     */

    public function upUser() {
        $userid = $_GET['userid'];
        $detail = new ManagingModule($userid);
        $details = $detail->sql_user('', false);
        if ($this->method == "POST") {
            $userid = $_POST['userid'];
            $user['iccard'] = $_POST['txtaudit'];
            $user['email'] = $_POST['txtemail'];
            $user['phone'] = $_POST['txtmobile'];
            $user['nickname'] = $_POST['nickname'];
            $user['department'] = $_POST['department'];
            $user['level'] = $_POST['level'];
            $user['ip_adress'] = $_POST['ip_adress'];
            switch ($detail->auth_user_update($user,$userid)) {
                case 1:
                    $this->auth_add_sql_log(2,$details[0]['username']);
                    Handler_tool::alertAndRedirect('该用户修改成功！', HTTPFILEURL . '/managing/selUserList/');
                    break;
                case 2:
                    Handler_tool::alertAndRedirect('邮箱格式填写错误！', HTTPFILEURL . '/managing/upUser/?userid=' . $userid );
                    break;
                default :
                    $this->auth_add_sql_log(6,$user['username']);
                    Handler_tool::alertAndRedirect('修改失败,请重新填写！', HTTPFILEURL . '/managing/upUser/?userid=' . $userid);
                    break;
            }
        }
        $this->tpl->assign('userid', $userid);
        $this->tpl->assign('detail', $details);
        $this->tpl->assign('dep_data', $detail->sql_department_data());
        $this->tpl->assign('level', UserConfig::$userlevel[$details[0]['level']]);
        $this->tpl->assign('userlevel', UserConfig::$userlevel);
        $this->tpl->display("managing/up_user.html");
    }
    
    /**
     * 删除用户
     */
    public function delEnableUser(){
        $userid = $_GET['userid'];
        $data=UserDao::getUserList($userid,false);
        $username=$data[0]['username'];
        $row = new ManagingModule($userid);
        if($row->auth_del_enable_user()){
          $msg = '用户删除操作成功!';
          $this->auth_add_sql_log(3,$username);
          Handler_tool::alertAndRedirect($msg, HTTPFILEURL.'/managing/selUserList/');
        }else{
            $msg = '用户删除操作失败!';
          $this->auth_add_sql_log(7,$username);
          Handler_tool::alertAndRedirect($msg, HTTPFILEURL.'/managing/selUserList/');
        }
    }

    /**
     * 记录用户操作审核日志
     */
    private function auth_add_sql_log($auth,$username){
        $type=2;
        switch ($auth){
            case 1:
                $msg ='添加用户成功';
                $type=1;
                break;
            case 2:
                $msg = '编辑用户成功';
                break;
            case 3:
                $msg = '删除用户成功';
                break;
            case 4:
                $msg = '批量添加用户成功';
                break;
            case 5:
                $msg = '添加用户失败';
                $type=1;
                break;
            case 6:
                $msg ='编辑用户失败';
                break;
            case 7:
                $msg ='删除用户失败';
                break;
            case 8:
                $msg ='批量添加用户失败';
                break;

        }
        $log_id=LogManager::writeAdminModifyLog($this->user['id'],$this->user['username'],$msg.$username);
        $auditlog=LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 1, $type);
        UserDao::sqlUpUserBakLog($username,$auditlog);
    }

}

?>